package com.yangyu.service;

import com.yangyu.currency.StatusCode;
import com.yangyu.mapper.BlogMapper;
import com.yangyu.mapper.LikeMapper;
import com.yangyu.message.BaseRsp;
import com.yangyu.pojo.Like;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 一碗鱼汤面
 * @version V1.0
 * @Package com.yangyu.service
 * @date 2020/12/20 21:32
 */
@Service
public class LikeService {
    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private BlogMapper blogMapper;

    /**
     * 新增点赞
     *
     * @param like
     * @return
     */
    public BaseRsp addLike(Like like) {
        try {
            likeMapper.addLike(like);
            blogMapper.likeBlog(like.getBlogId());
        } catch (Exception e) {
            return new BaseRsp(StatusCode.DB_ERROR, "add like error");
        }
        return new BaseRsp();
    }

    /**
     * 取消点赞
     *
     * @param like
     * @return
     */
    public BaseRsp cancelLike(Like like) {
        try {
            likeMapper.deleteLike(like);
            blogMapper.cancelBlog(like.getBlogId());
        } catch (Exception e) {
            return new BaseRsp(StatusCode.DB_ERROR, "delete like error");
        }
        return new BaseRsp();
    }

    /**
     * 根据userId和blogId查询是否点过赞
     *
     * @param like
     * @return
     */
    public BaseRsp getLike(Like like) {
        int likeNum;
        try {
            likeNum = likeMapper.getLike(like);
        } catch (Exception e) {
            return new BaseRsp(StatusCode.DB_ERROR, "delete like error");
        }
        return new BaseRsp(likeNum);
    }
}
